import * as actionTypes from "./constants";

import { 
  getRadioCategory,
  getRadioFine,
  getRadioRanking
} from "@/services/djradio";

const changeCategoryAction = (categories) => ({
  type: actionTypes.CHANGE_RADIO_CATEGORY,
  categories
});

const changeFineAction = (fine) => ({
  type: actionTypes.CHANGE_RADIO_FINE,
  fine
});

const changeRankingAction = (djRadios) => ({
  type: actionTypes.CHANGE_RADIO_RANKING,
  djRadios
});

export const changeCurrentPageAction = (page) => ({
  type: actionTypes.CHANGE_RANKING_PAGE,
  page
});

export const changeCurrentIdAction = (id) => ({
  type: actionTypes.CHANGE_RADIO_CURRENTID,
  id
});

/**
 * @returns 获取上方分类并且改变当前分类id
 */
export function getRadioCategoryAction() {
  return dispatch => {
    getRadioCategory().then(res => {
      dispatch(changeCategoryAction(res.categories));
      const cId = res.categories[0].id;
      dispatch(changeCurrentIdAction(cId));
    })
  }
};

/**
 * @param {当前分类id} currentId 
 * @returns 根据当前分类id获取优秀新电台 
 */
export function getRadioFineAction(currentId) {
  return dispatch => {
    getRadioFine(currentId).then(res => {
      dispatch(changeFineAction(res.djRadios));
    })
  }
};

/**
 * 
 * @param {当前分类id} currentId 
 * @returns 电台排行榜
 */
export function getRadioRankingAction(currentId) {
  return (dispatch, getState) => {
    let page = getState().getIn(["djradio", "currentPage"]);
    page = page - 1;
    getRadioRanking(currentId, page * 30, 30).then(res => {
      dispatch(changeRankingAction(res));
    })
  }
};
